import axios from '@/components/axios';

// create an axios instance
const service = axios.create({
	baseURL: '/', //测试环境
	//   baseURL: 'http://219.132.138.58:28087/api', //生产环境
	withCredentials: true, // send cookies when cross-domain requests
	method: 'get',
	showLoading: false,
	timeout: 100000 // request timeout
});

// request interceptor
service.interceptors.request.use(
	(config) => {
		if (sessionStorage.getItem('token')) {
			config.headers['Authorization'] = sessionStorage.getItem('token');
		}

		if (config.showLoading) {
			uni.showToast({
				icon: 'success',
				title: `请求中`
			});
		}
		return config;
	},
	(error) => {
		// do something with request error
		console.log(error); // for debug
		return error;
	}
);

// response interceptor
service.interceptors.response.use(
	/**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

	/**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
	(response) => {
		const res = response.data;
		return res;
	},
	(error) => {
		console.log('err' + error); // for debug
		return error;
	}
);

export default service;
